package com.edianping.store;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.edianping.util.LogCode;

public class ShopStatisticDBStore extends AbstractDBStore {
	protected static Logger log = Logger.getLogger(ShopDBStore.class);
	
	public static String[] GetShopInfoStatisticById(int shopId) {
		String[] result = {"","",""};
		
		Connection conn = null;
		Statement stmt = null;
		String sql = "select tag,paymethod,feature from shop_info_statistic where shop_id= " + shopId;
		
		try {
			conn = pool.getConnection();
			stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				result[0] = rs.getString("tag")==null?"":rs.getString("tag");
				result[1] = rs.getString("paymethod")==null?"":rs.getString("paymethod");
				result[2] = rs.getString("feature")==null?"":rs.getString("feature");
			}
			
			rs.close();
			stmt.close();
		} catch (Exception e) {
			log.error("[[" + LogCode.E_DB_SQL_ERROR + "]], SQL:[" + sql + "]");
			log.debug(e);
			if (log.isDebugEnabled())
				log.debug(e);
		} finally {
			if (conn != null)
				pool.checkin(conn);
		}
		
		return result;
	}
	
	public static boolean UpdateTagPaymethodFeature(int id, String tag,
			String paymethod, String feature) {
		Connection conn = null;
		Statement stmt = null;
		
		//check exist or not
		boolean isExist = false;
		String sql = "select count(*) as count from shop_info_statistic where shop_id="+id;
		try {
			conn = pool.getConnection();
			stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String strCount = rs.getString("count");
				if( Integer.parseInt( strCount ) > 0 ){
					isExist = true;
				}
			}
			
			rs.close();
			stmt.close();
		} catch (Exception e) {
			log.error("[[" + LogCode.E_DB_SQL_ERROR + "]], SQL:[" + sql + "]");
			log.debug(e);
			if (log.isDebugEnabled())
				log.debug(e);
		} finally {
			if (conn != null)
				pool.checkin(conn);
		}
		
		if( isExist ){
			sql = "update shop_info_statistic set " + "tag='" + tag + "'"
			+ ", paymethod='" + paymethod + "'" + ", feature='" + feature
			+ "'" + " where shop_id=" + id;
		}
		else{
			sql = "insert into shop_info_statistic (shop_id, tag, paymethod, feature ) values ( "
				+ id
				+ ", '" + tag + "'"
				+ ", '" + paymethod + "'" 
				+ ", '" + feature+ "'" + ")";
		}
		

		return ExecuteSQL(sql);
	}
}
